﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DotNetNuke.Modules.agPageAdmin;
using ScipBe.Common.LinqExtensions;

using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

namespace DotNetNuke.Silverlight.agPageAdmin.ViewModel
{
    public class PagesViewModel
    {
        public IEnumerable<HierarchyNode<PageInfo>> Pages { get; set; }

        //public void Load()
        //{
        //    PageContext ctx = new PageContext();
        //    ctx.Load<PageInfo>(ctx.GetPagesQuery(false));
        //    Pages = new PageCollection(ctx.PageInfos);
        //}

        public void LoadXml()
        {
            XDocument pagesXml = XDocument.Load("Data.xml");
            Pages = GetPages(pagesXml.Element("RootResults")).OrderBy(p => p.TabOrder).AsHierarchy(p => p.TabID, p => p.ParentId, -1);
        }

        private List<PageInfo> GetPages(XElement element)
        {
            return (from page in element.Elements("PageInfo")
                    select new PageInfo()
                    {
                        DisableLink = Boolean.Parse(page.Element("DisableLink").Value),
                        HasChildren = Boolean.Parse(page.Element("HasChildren").Value),
                        IconFile = page.Element("IconFile").Value,
                        IconFileLarge = page.Element("IconFileLarge").Value,
                        IsDeleted = Boolean.Parse(page.Element("IsDeleted").Value),
                        IsVisible = Boolean.Parse(page.Element("IsVisible").Value),
                        Level = int.Parse(page.Element("Level").Value),
                        ParentId = int.Parse(page.Element("ParentId").Value),
                        PortalID = int.Parse(page.Element("PortalID").Value),
                        TabID = int.Parse(page.Element("TabID").Value),
                        TabName = page.Element("TabName").Value,
                        TabOrder = int.Parse(page.Element("TabOrder").Value),
                        TabPath = page.Element("TabPath").Value,
                    }).ToList();
        }
    }
}
